package com.yunji.storage.context;

import com.yunji.common.exception.ServiceException;
import com.yunji.common.model.cache.ProductCacheDTO;
import com.yunji.common.utils.TimeUtils;
import com.yunji.order.dto.OrderDetailDTO;
import com.yunji.storage.domain.StorageRecordsDO;
import com.yunji.storage.domain.StorageTicketDO;

import java.math.BigDecimal;

public class StorageRecordsBuilder {

    private final StorageRecordsDO storageRecordsDO;

    private StorageRecordsBuilder() {
        this.storageRecordsDO = new StorageRecordsDO();
    }

    public static StorageRecordsBuilder builder() {
        return new StorageRecordsBuilder();
    }

    public StorageRecordsBuilder buildTicket(StorageTicketDO ticketDO) {
        ServiceException.throwException(ticketDO == null, "出入库单据为空");
        storageRecordsDO.setBelongId(ticketDO.getBelongId());
        storageRecordsDO.setTicketId(ticketDO.getTicketId());
        storageRecordsDO.setTicketCode(ticketDO.getTicketCode());
        storageRecordsDO.setBusinessId(ticketDO.getBusinessId());
        storageRecordsDO.setBusinessCode(ticketDO.getBusinessCode());
        storageRecordsDO.setStorageType(ticketDO.getStorageType());
        storageRecordsDO.setOperateTime(TimeUtils.getNowDate());
        return this;
    }

    public StorageRecordsBuilder buildOrder(OrderDetailDTO dto) {
        ServiceException.throwException(dto == null, "订单详情为空");
        storageRecordsDO.setProductId(dto.getProductId());
        storageRecordsDO.setExpectQuantity(BigDecimal.valueOf(dto.getProductNum()));
        storageRecordsDO.setActualQuantity(BigDecimal.ZERO);
        return this;
    }

    public StorageRecordsBuilder buildProduct(ProductCacheDTO product) {
        ServiceException.throwException(product == null, "商品为空");
        storageRecordsDO.setProductName(product.getProductName());
        storageRecordsDO.setProductSku(product.getSku());
        storageRecordsDO.setUnitId(product.getUnitId());
        return this;
    }

    public StorageRecordsDO build(){
        return this.storageRecordsDO;
    }


}
